import { IDEFormMDCtrl } from '@ibiz/model-core';
import { FormDetailController } from '../form-detail';
import { FormMDCtrlContentTypeController } from './form-mdctrl-content-type.controller';
import { FormMDCtrlDataViewController } from './form-mdctrl-data-view/form-mdctrl-data-view.controller';
import { FormMDCtrlFormController } from './form-mdctrl-form/form-mdctrl-form.controller';
import { FormMDCtrlGridController } from './form-mdctrl-grid/form-mdctrl-grid.controller';
import { FormMDCtrlListController } from './form-mdctrl-list/form-mdctrl-list.controller';
import { FormMDCtrlRepeaterFormOneController } from './form-mdctrl-repeater/form-mdctrl-repeater-form-one/form-mdctrl-repeater-form-one.controller';
import { FormMDCtrlRepeaterFormController } from './form-mdctrl-repeater/form-mdctrl-repeater-form/form-mdctrl-repeater-form.controller';
import { FormMDCtrlRepeaterGridController } from './form-mdctrl-repeater/form-mdctrl-repeater-grid/form-mdctrl-repeater-grid.controller';
import { FormMDCtrlState } from './form-mdctrl.state';

/**
 * 表单多数据部件控制器
 *
 * @author zk
 * @date 2023-08-02 09:08:55
 * @export
 * @class FormMDCtrlController
 * @extends {FormDetailController<IDEFormMDCtrl>}
 */
export class FormMDCtrlController extends FormDetailController<IDEFormMDCtrl> {
  /**
   * 表单多数据部件控制器状态
   *
   * @author zk
   * @date 2023-07-28 09:07:14
   * @type {FormMDCtrlState}
   * @memberof FormMDCtrlController
   */
  declare state: FormMDCtrlState;

  /**
   * Creates an instance of FormMDCtrlController.
   *
   * @author zk
   * @date 2023-07-28 09:07:25
   * @protected
   * @return {*}  {FormMDCtrlState}
   * @memberof FormMDCtrlController
   */
  protected createState(): FormMDCtrlState {
    return new FormMDCtrlState(this.parent?.state);
  }

  /**
   * 表单多数据部件内容控制器
   *
   * @author zk
   * @date 2023-07-28 11:07:49
   * @type {(FormMDCtrlFormController
   *     | FormMDCtrlGridController)}
   * @memberof FormMDCtrlController
   */
  public mdCtrlContentController!: FormMDCtrlContentTypeController;

  /**
   * 初始化
   *
   * @author zk
   * @date 2023-07-25 10:07:11
   * @protected
   * @return {*}  {Promise<void>}
   * @memberof FormMDCtrlController
   */
  protected async onInit(): Promise<void> {
    await super.onInit();

    switch (this.model.contentType) {
      case 'FORM':
        this.mdCtrlContentController = new FormMDCtrlFormController(this);
        break;
      case 'GRID':
        this.mdCtrlContentController = new FormMDCtrlGridController(this);
        break;
      case 'LIST':
        this.mdCtrlContentController = new FormMDCtrlListController(this);
        break;
      case 'DATAVIEW':
        this.mdCtrlContentController = new FormMDCtrlDataViewController(this);
        break;
      case 'REPEATER':
        if (this.model.detailStyle === 'DEFAULT') {
          this.mdCtrlContentController = new FormMDCtrlRepeaterFormController(
            this,
          );
        }
        if (this.model.detailStyle === 'STYLE3') {
          this.mdCtrlContentController =
            new FormMDCtrlRepeaterFormOneController(this);
        }
        if (this.model.detailStyle === 'STYLE2') {
          this.mdCtrlContentController = new FormMDCtrlRepeaterGridController(
            this,
          );
        }
        break;
      default:
        break;
    }
    if (this.mdCtrlContentController) {
      await this.mdCtrlContentController.onInit();
    }
  }

  /**
   * 手动通知外部表单值变更
   *
   * @author zk
   * @date 2023-08-02 09:08:41
   * @memberof FormMDCtrlController
   */
  setFormDataValue(): void {
    const name =
      this.model.appDEFieldId || this.model.fieldName || this.model.id;
    if (name) {
      this.form.setDataValue(name, this.state.contentCtrlData);
    }
  }

  /**
   * 加载数据，要在表单加载完成之后再去加载
   *
   * @author zk
   * @date 2023-07-28 11:07:09
   * @memberof FormMDCtrlController
   */
  async loadData(): Promise<void> {
    await this.mdCtrlContentController.initData();
  }
}
